using CoreManager.Elasticsearch.Models;
using CoreManager.Elasticsearch.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace CoreManager.Elasticsearch.Extensions;

/// <summary>
/// 服务集合扩展
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// 添加Elasticsearch服务
    /// </summary>
    /// <param name="services">服务集合</param>
    /// <param name="configuration">配置</param>
    /// <returns></returns>
    public static IServiceCollection AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
    {
        // 配置选项
        services.Configure<ElasticsearchOptions>(configuration.GetSection(ElasticsearchOptions.SectionName));
        
        // 注册服务
        services.AddSingleton<IElasticsearchService, ElasticsearchService>();
        
        return services;
    }
    
    /// <summary>
    /// 添加Elasticsearch服务
    /// </summary>
    /// <param name="services">服务集合</param>
    /// <param name="configureOptions">配置选项</param>
    /// <returns></returns>
    public static IServiceCollection AddElasticsearch(this IServiceCollection services, Action<ElasticsearchOptions> configureOptions)
    {
        services.Configure(configureOptions);
        
        // 注册服务
        services.AddSingleton<IElasticsearchService, ElasticsearchService>();
        
        return services;
    }
}

